package com.liu.mongodb.demo.service.impl;

import com.liu.mongodb.demo.bean.PageVO;
import com.liu.mongodb.demo.entity.Student;
import com.liu.mongodb.demo.service.StudentService;
import javafx.scene.shape.Circle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;

@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    MongoTemplate mongoTemplate;

    @Override
    public Student insertStudent(Student student) {
        Student student1 = mongoTemplate.insert(student);
//        Student student1 = mongoTemplate.save(student);//
        return student1;
    }

    @Override
    public ArrayList<Student> insertStudents(ArrayList<Student> students) {
        ArrayList<Student> students1 = (ArrayList<Student>) mongoTemplate.insertAll(students);
        return students1;
    }

    @Override
    public Student updataStudent(Student student) {
        //如果_id存在，使用save进行更新
        Student student1 = mongoTemplate.save(student);
        return student1;
    }

    @Override
    public Student updataStudentByfield(Student student){
        Query query = new Query(Criteria.where("_id").is(student.getId()));
        Update update = new Update();
        update.set("name",student.getName()).set("age",student.getAge());
        Student student1 = mongoTemplate.findAndModify(query,update,Student.class);
        return student1;
    }

    @Override
    public Student removeStudent(long id) {

        Query query = new Query(Criteria.where("_id").is(id));
        Student student = mongoTemplate.findAndRemove(query,Student.class);
        return student;
    }

    @Override
    public ArrayList<Student> removeAllStudent(Long... id) {
        Query query = new Query(Criteria.where("_id").in(id));
        ArrayList<Student> students = (ArrayList<Student>) mongoTemplate.findAllAndRemove(query,Student.class);
        return students;
    }

    @Override
    public Student findOneStudent(long id) {
        Query query = new Query(Criteria.where("_id").is(id));
        Student student = mongoTemplate.findOne(query,Student.class);
        return student;
    }

    @Override
    public long countOfStudent() {
        Query query = new Query();
        long cout = mongoTemplate.count(query,Student.class);
        return cout;
    }

    @Override
    public List<Student> findAllStudent() {
        Pattern pattern = Pattern.compile("^.*武当山.*$", Pattern.CASE_INSENSITIVE);
        Query query = new Query(Criteria.where("address").regex(pattern));
        List<Student> students = mongoTemplate.find(query,Student.class);
        return students;
    }

    @Override
    public PageVO<Student> findSomeStudent(Integer pageIndex, Integer pageSize,String address) {
        Query query = new Query();
        Pattern pattern = Pattern.compile("^.*"+address+".*$", Pattern.CASE_INSENSITIVE);
        query.addCriteria(Criteria.where("address").regex(pattern));
        // 1.根据条件查询总数 总数无数据则反回空数据分页对象
        int count = (int) mongoTemplate.count(query, Student.class);
        if (count < 1) {
            return PageVO.emptyResult();
        }
        //2.按年龄倒序
        query.with(Sort.by(Sort.Direction.DESC, "age"));
        //3.通过skip和limit做分页
        int skip = (pageIndex - 1) * pageSize;
        query.skip(skip).limit(pageSize);
        List<Student> students = mongoTemplate.find(query, Student.class);
        return  PageVO.getPageResult(students, pageIndex, pageSize, count);
    }

}
